<?php

/**写入开奖结果 **/

/**会预加载一份下期开奖的开奖号码为空的新数据到kjjg_list 表中**/

set_time_limit(0);
error_reporting(0);
date_default_timezone_set("PRC");
require 'db_config.php';

//1.连接数据库->pdo方式
$db_pdo_link = pdo_write_read_link();  //定义为全局变量

if(!$db_pdo_link)
{
	die('pdo connection failed!');
}


	$GET_URL_P="http://114.215.68.205/ffc_getkjjg.php";

	$GET_ZHI_VP = file_get_contents($GET_URL_P);

	$date =json_decode($GET_ZHI_VP);	
	
	/*$redis = new Redis();
	$redis->connect('10.31.74.145','6379');
	//$redis->connect('127.0.0.1','6379');
	$redis->auth('Redis');
	$GET_ZHI_VP = $redis->get("ffc_kjjg");*/

//  处理分分彩	写入开奖结果 
	 
	ECHO "开始处理系统彩种的开奖</BR>";
	OPENKJJG_RU_SQL($date);

//  分分彩开奖数据入库操作
FUNCTION OPENKJJG_RU_SQL($kj){
	 
	 global $db_pdo_link;
	 
	  $CZID=$kj->czid;
	  $KJJGNS=$kj->opencode;
	  $QIHAO= $kj->qihao;
	  $opentimestamp= $kj->opentimestamp;


	  $IFCUNZQISHU = $db_pdo_link->query("SELECT * FROM kkty_kjjg_list WHERE qihao='$QIHAO' AND czid=62")->fetch(PDO::FETCH_ASSOC);

	 IF($IFCUNZQISHU['kjjg']<1){
		 
		 ECHO "编号为：【62】的彩票第：【".$QIHAO."】期正在进行开奖！</BR>";
	 
		 IF($IFCUNZQISHU['id']>0){
			 
			 IF($IFCUNZQISHU['iftz']<1){
				 
				 $db_pdo_link->exec("UPDATE kkty_kjjg_list SET `kjjg`='$KJJGNS',`iftz`='1' WHERE id={$IFCUNZQISHU['id']}");				 
			 }
			 
		 }ELSE{
			 
			 $NTIMS=TIME();
			 
			 $MYSQL_ZHIXING_YUJU_A="`czid`,`qihao`,`jztime`,`kjjg`,`iftz`";
		 
			 $MYSQL_ZHIXING_YUJU_B="'62','$QIHAO','$NTIMS','$KJJGNS','1'";
		 
			 $db_pdo_link->exec("INSERT INTO `kkty_kjjg_list` ($MYSQL_ZHIXING_YUJU_A) VALUES ($MYSQL_ZHIXING_YUJU_B)");
		 }
		 
	 }
	 
	 //生成新的一期
	 add_kjjg(time());
	 
	 if($QIHAO && $CZID == 62) {
	     //检查追号是否存在本期
	     $exist = $db_pdo_link->query("SELECT * FROM kkty_touzhu_list_zh WHERE czid=62 AND qihao='$QIHAO'")->fetchAll(PDO::FETCH_ASSOC);
	      foreach($exist as $ve) {
	          $trace_field = "`czid`,`qihao`,`tzhm`,`tzje`,`dzje`,`tzzs`,`username`,`tzhje`,`zjje`,`ifqs`,`wfid`,`dqpl`,`xzfd`,`fdje`,`kjjg`,`zjzs`,`nbzh`,`tz_time`";
	          $trace_value = "'{$ve[czid]}','{$ve[qihao]}','{$ve[tzhm]}','{$ve[tzje]}','{$ve[dzje]}','{$ve[tzzs]}','{$ve[username]}','{$ve[tzhje]}','{$ve[zjje]}','{$ve[ifqs]}','{$ve[wfid]}','{$ve[dqpl]}','{$ve[xzfd]}','{$ve[fdje]}','{$ve[kjjg]}','{$ve[zjzs]}','{$ve[nbzh]}','{$ve[tz_time]}'";
	          $db_pdo_link->exec("INSERT INTO `kkty_touzhu_list_no` ($trace_field) VALUES ($trace_value)");
	          $db_pdo_link->exec("DELETE FROM `kkty_touzhu_list_zh` WHERE id={$ve['id']}");
	      }
	 }
	 
}


function add_kjjg($sj){

    global $db_pdo_link;
    $ALLR=$db_pdo_link->query("SELECT * FROM kkty_time WHERE czid=62 order by id asc")->fetchAll(PDO::FETCH_ASSOC);

    $IFZXWB="1";
    foreach ($ALLR as $item){
        if($sj < strtotime(date('Y-m-d').$item['jztime'])){
            NEW_CAIPIAO('62', date('YmdHi',strtotime(date('Y-m-d').$item['jztime'])), strtotime(date('Y-m-d').$item['jztime']), '');
            break;
        }
    }
}


FUNCTION NEW_CAIPIAO($CZID,$QIHAO,$JZTIME,$PSS){

    global $db_pdo_link;

    IF($PSS==""){$PSS="官方彩无预设";}

    $IFCUNQIHAO=$db_pdo_link->query("SELECT * FROM kkty_kjjg_list WHERE czid=$CZID AND qihao='$QIHAO'")->fetch(PDO::FETCH_ASSOC);

    IF($IFCUNQIHAO['id']<1){
        	
        $MYSQL_ZHIXING_YUJU_A="`czid`,`qihao`,`jztime`,`kjjg`,`iftz`,`ps`";

        $MYSQL_ZHIXING_YUJU_B="'$CZID','$QIHAO','$JZTIME','0','0','$PSS'";
        	
        echo "INSERT INTO `kkty_kjjg_list` ($MYSQL_ZHIXING_YUJU_A) VALUES ($MYSQL_ZHIXING_YUJU_B)";echo "\rn";

        $db_pdo_link->exec("INSERT INTO `kkty_kjjg_list` ($MYSQL_ZHIXING_YUJU_A) VALUES ($MYSQL_ZHIXING_YUJU_B)");
        	

	}
}

